package com.saturn.action.sldb.emergency;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.sldb.Emergency;

public class QueryTemp2Action implements IAction{
	@Override
	public IView execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		String view = request.getParameter("view");
		String id = request.getParameter("id");
		Emergency vo = Emergency.get(id);
		String type="";
		
		request.setAttribute("emergency", vo);
		if("ok".equals(view)){
			return new JspView("/app/sldb/emergency/approval/view.jsp");
		}if("files".equals(view)){
			Emergency vo1 = Emergency.get(id);
			request.setAttribute("emergency", vo1);
			type = "1";
			return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
		}else if("filesByAgo".equals(view)){
			SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
			String createTimeFormat="";
			try {
				createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String nowTimeStr = DateUtils.getSystemDate();
			String nowTimeFormat="";
			try {
				nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int createTime = Integer.parseInt(createTimeFormat);
			int nowTime = Integer.parseInt(nowTimeFormat);
			if(nowTime-createTime>=200){
				return new JspView("/app/sldb/emergency/apply/timeOut.jsp");
			}else{
				Emergency vo1 = Emergency.get(id);
				request.setAttribute("emergency", vo1);
				type = "4";
				return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
			}
		}else if("filesByAutoApply".equals(view)){
			Emergency vo1 = Emergency.get(id);
			request.setAttribute("emergency", vo1);
			type = "0";
			return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
		}else if("filesByApprovalAgo".equals(view)){
			SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
			String createTimeFormat="";
			try {
				createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String nowTimeStr = DateUtils.getSystemDate();
			String nowTimeFormat="";
			try {
				nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int createTime = Integer.parseInt(createTimeFormat);
			int nowTime = Integer.parseInt(nowTimeFormat);
			if(nowTime-createTime>=200){
				return new JspView("/app/sldb/emergency/apply/timeOut.jsp");
			}else{
				Emergency vo1 = Emergency.get(id);
				request.setAttribute("emergency", vo1);
				type = "5";
				return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
			}
		}else if("filesByApply".equals(view)){
			SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
			String createTimeFormat="";
			try {
				createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String nowTimeStr = DateUtils.getSystemDate();
			String nowTimeFormat="";
			try {
				nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int createTime = Integer.parseInt(createTimeFormat);
			int nowTime = Integer.parseInt(nowTimeFormat);
			if(nowTime-createTime>=200){
				return new JspView("/app/sldb/emergency/apply/timeOut.jsp");
			}else{
				Emergency vo1 = Emergency.get(id);
				request.setAttribute("emergency", vo1);
				type = "2";
				return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
			}
			
		}else if("filesByApproval".equals(view)){
			SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
			String createTimeFormat="";
			try {
				createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String nowTimeStr = DateUtils.getSystemDate();
			String nowTimeFormat="";
			try {
				nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int createTime = Integer.parseInt(createTimeFormat);
			int nowTime = Integer.parseInt(nowTimeFormat);
			if(nowTime-createTime>=200){
				return new JspView("/app/sldb/emergency/apply/timeOut.jsp");
			}else{
				Emergency vo1 = Emergency.get(id);
				request.setAttribute("emergency", vo1);
				type = "3";
				return new JspView("/app/sldb/emergency/approval/upfile.jsp?type="+type);
			}
			
		}else{
			return new JspErrorView("上传附件功能有误！");
		}
	}
	
	@Override
	public String requestMapping() {
		return "/app/sldb/emergency/queryTemp2.action";
	}
}
